<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <style>
    .box1 {
      position: absolute;
      width: 200px;
      height: 200px;
      background-color: skyblue;
    }
  </style>
  <body style="height: 4000px">
    <div class="box1"></div>

    <script>
      var box = document.querySelector(".box1");

      document.onmousemove = function () {
        var st =
          document.documentElement.scrollLeft || document.body.scrollLeft; //不加body也可以,谷歌已支持
        var vt = document.documentElement.scrollTop || document.body.scrollTop;
        var left = event.clientX;
        var top = event.clientY;
        // var left = event.pageX;
        // var top = event.pageY;

        box.style.left = left + st + "px";
        box.style.top = top + vt + "px";
      };
    </script>
  </body>
</html>
